<?php
// +----------------------------------------------------------------------
// | 麦沃德科技赋能开发者，助力中小企业发展
// +----------------------------------------------------------------------
// | Copyright (c) 2017～2024  www.wdadmin.cn    All rights reserved.
// +----------------------------------------------------------------------
// | Wdadmin系统产品软件并不是自由软件，不加密，并不代表开源，未经许可不可自由转售和商用
// +----------------------------------------------------------------------
// | Author: MY WORLD Team <bd@maiwd.cn>   www.wdadmin.cn
// +----------------------------------------------------------------------
/**
 * Class dashboard
 * Desc  数据概览控制器
 * Create on 2024/11/13 10:38
 * Create by wangyafang
 */

namespace app\admin\controller\wddj;



use app\admin\model\wddj\Driver;
use app\admin\model\wddj\finance\IncomeExpenditure;
use app\admin\model\wddj\Order;
use app\admin\model\wddj\Payments;
use app\admin\model\wddj\user\Wechat;
use app\common\controller\Backend;
use fast\Date;


class Dashboard extends Backend
{
    /**
     * 查看
     */
    public function index()
    {
        //交易数据
        $orderModel = new Order();
        $totalStateArray = ['pending','accepted','in_progress','adjusting','refunded','awaiting_payment','completed'];
        $total_transaction_volume_platform_orders = $orderModel->where('state','in',$totalStateArray)->sum('pay_amount');
        $total_order_quantity = $orderModel->where('state','in',$totalStateArray)->count();
        $incomeExpenditureModel = new IncomeExpenditure();
        $platform_revenue = $incomeExpenditureModel->where('operate','1')->where('group_type','2')->sum('amount');
        $paymentsModel = new Payments();
        $today_transaction_volume_platform_orders = $paymentsModel
            ->whereTime('pay_time', 'today')
            ->where('payment_status','completed')
            ->sum('pay_amount');
        $month_transaction_volume_platform_orders = $paymentsModel
            ->whereTime('pay_time', 'month')
            ->where('payment_status','completed')
            ->sum('pay_amount');
        $year_transaction_volume_platform_orders = $paymentsModel
            ->whereTime('pay_time', 'year')
            ->where('payment_status','completed')
            ->sum('pay_amount');
        $today_order_quantity = $orderModel
            ->where('state','in',['pending','accepted','in_progress','adjusting','refunded','awaiting_payment','completed','cancelled'])
            ->whereTime('createtime', 'today')
            ->count();
        $month_order_quantity = $orderModel
            ->where('state','in',['pending','accepted','in_progress','adjusting','refunded','awaiting_payment','completed','cancelled'])
            ->whereTime('createtime', 'month')
            ->count();
        $year_order_quantity = $orderModel
            ->where('state','in',['pending','accepted','in_progress','adjusting','refunded','awaiting_payment','completed','cancelled'])
            ->whereTime('createtime', 'year')
            ->count();
        $this->view->assign([
            'total_transaction_volume_platform_orders'         => $total_transaction_volume_platform_orders,
            'total_order_quantity'         => $total_order_quantity,
            'platform_revenue'         => $platform_revenue,
            'today_transaction_volume_platform_orders'         => $today_transaction_volume_platform_orders,
            'month_transaction_volume_platform_orders'         => $month_transaction_volume_platform_orders,
            'year_transaction_volume_platform_orders'         => $year_transaction_volume_platform_orders,
            'today_order_quantity'         => $today_order_quantity,
            'month_order_quantity'         => $month_order_quantity,
            'year_order_quantity'         => $year_order_quantity,
        ]);

        //基础数据
        $userWechatModel = new Wechat();
        $total_number_users = $userWechatModel->count();
        $today_number_users = $userWechatModel->whereTime('createtime', 'today')->count();
        $month_number_users = $userWechatModel->whereTime('createtime', 'month')->count();
        $year_number_users = $userWechatModel->whereTime('createtime', 'year')->count();
        $this->view->assign([
            'total_number_users'         => $total_number_users,
            'today_number_users'         => $today_number_users,
            'month_number_users'         => $month_number_users,
            'year_number_users'         => $year_number_users,
        ]);
        $driverModel = new Driver();
        $total_number_driver = $driverModel->count();
        $today_number_driver = $driverModel->whereTime('createtime', 'today')->count();
        $month_number_driver = $driverModel->whereTime('createtime', 'month')->count();
        $year_number_driver = $driverModel->whereTime('createtime', 'year')->count();
        $certifiedDrivingAgentModel = new \app\admin\model\wddj\CertifiedDrivingAgent();
        $number_pending_audits = $certifiedDrivingAgentModel->where('state','1')->count();
        $verified_quantity = $certifiedDrivingAgentModel->where('state','2')->count();
        $this->view->assign([
            'total_number_driver'         => $total_number_driver,
            'today_number_driver'         => $today_number_driver,
            'month_number_driver'         => $month_number_driver,
            'year_number_driver'         => $year_number_driver,
            'number_pending_audits'         => $number_pending_audits,
            'verified_quantity'         => $verified_quantity,
        ]);
        $column = [];
        $starttime = Date::unixtime('day', -6);
        $endtime = Date::unixtime('day', 0, 'end');
        $user_wechat_create_list = $userWechatModel->where('createtime', 'between time', [$starttime, $endtime])
            ->field('createtime, COUNT(*) AS nums, DATE_FORMAT(FROM_UNIXTIME(createtime), "%Y-%m-%d") AS create_date')
            ->group('create_date')
            ->select();
        $driver_create_list = $driverModel->where('createtime', 'between time', [$starttime, $endtime])
            ->field('createtime, COUNT(*) AS nums, DATE_FORMAT(FROM_UNIXTIME(createtime), "%Y-%m-%d") AS create_date')
            ->group('create_date')
            ->select();

        for ($time = $starttime; $time <= $endtime;) {
            $column[] = date("Y-m-d", $time);
            $time += 86400;
        }
        $user_wechat_list = array_fill_keys($column, 0);
        $driver_list = array_fill_keys($column, 0);
        foreach ($user_wechat_create_list as $k => $v) {
            $user_wechat_list[$v['create_date']] = $v['nums'];
        }
        foreach ($driver_create_list as $k => $v) {
            $driver_list[$v['create_date']] = $v['nums'];
        }

        $this->assignconfig('user_wechat_data', array_values($user_wechat_list));
        $this->assignconfig('driver_data', array_values($driver_list));

        //订单数据
        $pending_count = $orderModel->where('state','pending')->count();
        $in_progress_count = $orderModel->where('state','in',['accepted','in_progress'])->count();
        $completed_count = $orderModel->where('state','completed')->count();
        $cancelled_count = $orderModel->where('state','cancelled')->count();
        $this->assignconfig('pending_count', $pending_count);
        $this->assignconfig('in_progress_count', $in_progress_count);
        $this->assignconfig('completed_count', $completed_count);
        $this->assignconfig('cancelled_count', $cancelled_count);
        $column = [];
        $starttime = strtotime('Monday this week');
        $endtime = strtotime('Sunday this week');
        $order_create_list = $orderModel->where('createtime', 'between time', [$starttime, $endtime])
            ->where('state','in',['pending','accepted','in_progress','adjusting','refunded','awaiting_payment','completed','cancelled'])
            ->field('createtime, COUNT(*) AS nums, DATE_FORMAT(FROM_UNIXTIME(createtime), "%Y-%m-%d") AS create_date')
            ->group('create_date')
            ->select();
        for ($time = $starttime; $time <= $endtime;) {
            $column[] = date("Y-m-d", $time);
            $time += 86400;
        }
        $order_list = array_fill_keys($column, 0);
        foreach ($order_create_list as $k => $v) {
            $order_list[$v['create_date']] = $v['nums'];
        }
        $this->assignconfig('order_data', array_values($order_list));

        $this->assignconfig('column', array_keys($user_wechat_list));

        return $this->view->fetch();
    }
}



 